package com.platform.api.bzproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;

import com.platform.api.bzproject.model.BzFavorite;
import com.platform.api.bzproject.service.BzFavoriteService;
import com.platform.api.bzproject.vo.BzWallpaperVo;
import com.platform.common.core.Constant;
import com.platform.common.core.Result;
import com.platform.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guoyunfei on 2021/7/11 15:45
 * @className BzFavoriteController
 * @description TODO
 * @Version 1.0
 * 源码联系微信：2549789059
 */
@Api(tags = "我的收藏，点赞，浏览")
@RestController
@RequestMapping("{appid}/p/favorite")
public class BzFavoriteController {

    @Autowired
    private BzFavoriteService bzFavoriteService;

    @ApiOperation(value = "我的收藏，点赞，浏览", notes="我的收藏，点赞，浏览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数，默认12", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/getMyData")
    private Result getMyData(BzWallpaperVo wallpaper, Integer pageNum, Integer pageSize, @PathVariable String appid){
        //wallpaper.setAppid(appid);
        String userId = null;
        try {
            userId = SecurityUtils.getUser().getUserId();
            if(StringUtils.isNotBlank(userId)){
                wallpaper.setOpenid(userId);
            }
        }catch (Exception e){

        }
        if(null == pageNum){
            pageNum = 1;
        }
        if(null == pageSize){
            pageSize = Constant.PAGESIZE_WALlPAPER;
        }
        PageInfo<BzWallpaperVo> pageInfo = bzFavoriteService.getMyData(wallpaper, pageNum, pageSize);
        List<BzWallpaperVo> list = pageInfo.getList();

        pageInfo.setList(list);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "我的收藏，点赞，浏览-新增采集数据", notes="我的收藏，点赞，浏览-新增采集数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "条数，默认12", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "openid", value = "用户微信openid", required = true, dataType = "String"),
            @ApiImplicitParam(name = "appid", value = "小程序appid", required = true, dataType = "String"),
    })
    @GetMapping("/getMyDataNew")
    private Result getMyDataNew(BzWallpaperVo wallpaper, Integer pageNum, Integer pageSize){

        String userId = null;
        try {
            userId = SecurityUtils.getUser().getUserId();
            if(StringUtils.isNotBlank(userId)){
                wallpaper.setOpenid(userId);
            }
        }catch (Exception e){

        }

        if(null == pageNum){
            pageNum = 1;
        }
        if(null == pageSize){
            pageSize = Constant.PAGESIZE_WALlPAPER;
        }
        PageInfo<BzWallpaperVo> pageInfo = bzFavoriteService.getMyDataNew(wallpaper, pageNum, pageSize);
        List<BzWallpaperVo> list = pageInfo.getList();
        pageInfo.setList(list);
        return Result.success(pageInfo);
    }

    @PostMapping("/saveOrUpdate")
    private Result saveOrUpdate(@RequestBody BzFavorite favorite){

        String userId = null;
        try {
            userId = SecurityUtils.getUser().getUserId();
        }catch (Exception e){

        }
        if(StringUtils.isEmpty(userId)){
            return Result.failure("用户信息获取失败");
        }

        favorite.setOpenid(userId);
        Map<String,Object> map = new HashMap<>();

        BzFavorite bzFavorite = bzFavoriteService.getOne(new QueryWrapper<BzFavorite>().eq("openid", favorite.getOpenid())
                .eq("wallpaper_id", favorite.getWallpaperId())
                .eq("type", favorite.getType()));

        if(null != bzFavorite){
            //删除点赞或者收藏信息
            bzFavoriteService.deleteByWallpaperId(favorite.getWallpaperId(),favorite.getOpenid(),favorite.getType(),null);

            //获取是否点赞和收藏
            BzFavorite bzFavorite1 = bzFavoriteService.getOne(
                    new QueryWrapper<BzFavorite>().eq("openid", favorite.getOpenid())
                            .eq("wallpaper_Id",favorite.getWallpaperId())
                            .eq("type",1)
            );
            map.put("post_favorite",null != bzFavorite1 ? 1 : 0);
            BzFavorite bzFavorite2 = bzFavoriteService.getOne(
                    new QueryWrapper<BzFavorite>().eq("openid", favorite.getOpenid())
                            .eq("wallpaper_Id",favorite.getWallpaperId())
                            .eq("type",2)
            );
            map.put("post_like",null != bzFavorite2 ? 1 : 0);
            return Result.success(map);
        }

        //favorite.setAppid(appid);
        favorite.setCreateTime(new Date());
        bzFavoriteService.save(favorite);

        //获取是否点赞和收藏
        bzFavorite = bzFavoriteService.getOne(
                new QueryWrapper<BzFavorite>().eq("openid", favorite.getOpenid())
                        .eq("wallpaper_Id",favorite.getWallpaperId())
                        .eq("type",1)
        );
        map.put("post_favorite",null != bzFavorite ? 1 : 0);
        bzFavorite = bzFavoriteService.getOne(
                new QueryWrapper<BzFavorite>().eq("openid", favorite.getOpenid())
                        .eq("wallpaper_Id",favorite.getWallpaperId())
                        .eq("type",2)
        );
        map.put("post_like",null != bzFavorite ? 1 : 0);

        return Result.success(map);
    }



}
